package com.wonderful.mall.product.service.impl;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.wonderful.mall.product.entity.AttrAttrgroupRelationEntity;
import com.wonderful.mall.product.entity.AttrEntity;
import com.wonderful.mall.product.entity.ProductAttrValueEntity;
import com.wonderful.mall.product.service.AttrAttrgroupRelationService;
import com.wonderful.mall.product.service.AttrService;
import com.wonderful.mall.product.service.ProductAttrValueService;
import com.wonderful.mall.product.vo.*;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wonderful.common.utils.PageUtils;
import com.wonderful.common.utils.Query;

import com.wonderful.mall.product.dao.AttrGroupDao;
import com.wonderful.mall.product.entity.AttrGroupEntity;
import com.wonderful.mall.product.service.AttrGroupService;

import javax.annotation.Resource;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {
    @Resource
    private AttrService attrService;

    @Resource
    private ProductAttrValueService productAttrValueService;

    @Resource
    private AttrAttrgroupRelationService attrAttrgroupRelationService;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }


    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catelogId) {
        String key = (String) params.get("key");
        QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(key)) {
            wrapper.and((obj) -> obj.like("attr_group_name", key).or().eq("attr_group_id", key));
        }
        if (catelogId == 0) {
            //如果传过来的id是0，则查询所有属性
            //this.page两个参数，第一个参数是查询页码信息，其中Query.getPage方法传入一个map，会自动封装成IPage
            //第二个参数是查询条件，空的wrapper就是查询全部
            IPage<AttrGroupEntity> page = this.page(
                    new Query<AttrGroupEntity>().getPage(params),
                    wrapper);
            return new PageUtils(page);
        } else {
            wrapper.eq("catelog_id", catelogId);
            IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params),
                    wrapper);
            return new PageUtils(page);
        }
    }

    //根据分类id查出所有的分组以及组属性
    @Override
    public List<AttrGroupWithAttrsVo> getAttrGroupWithAttrsByCatelogId(Long catelogId) {
        //查询分组信息
        List<AttrGroupEntity> attrGroupEntities = this.list(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId));
        return attrGroupEntities.stream().map((item)->{
            AttrGroupWithAttrsVo vo = new AttrGroupWithAttrsVo();
            BeanUtils.copyProperties(item,vo);
            List<AttrEntity> relationAttr = attrService.getRelationAttr(vo.getAttrGroupId());
            vo.setAttrs(relationAttr);
            return vo;
        }).collect(Collectors.toList());
    }

    @Override
    public List<SpuItemAttrGroupVo> getAttrGroupWithAttrsBySpuId(Long spuId, Long catalogId) {
        List<SpuItemAttrGroupVo> list = new ArrayList<>();

        List<AttrGroupEntity> attrGroupEntities = this.getBaseMapper().selectList(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catalogId));

        for (AttrGroupEntity item : attrGroupEntities) {
            String attrGroupName = item.getAttrGroupName();
            Long attrGroupId = item.getAttrGroupId();
            List<AttrAttrgroupRelationEntity> attrgroupRelationEntities = attrAttrgroupRelationService.getBaseMapper().selectList(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_group_id", attrGroupId));
            List<Attr> attrs = attrgroupRelationEntities.stream().map(attrAttrgroupRelationEntity -> {
                Long attrId = attrAttrgroupRelationEntity.getAttrId();
                AttrEntity attrEntity = attrService.getBaseMapper().selectOne(new QueryWrapper<AttrEntity>().eq("attr_id", attrId));
                String attrName = attrEntity.getAttrName();
                ProductAttrValueEntity productAttrValueEntity = productAttrValueService.getBaseMapper().selectOne(new QueryWrapper<ProductAttrValueEntity>().eq("spu_id", spuId).eq("attr_id",attrId));
                if (productAttrValueEntity!=null){
                    return new Attr(attrId, attrName, productAttrValueEntity.getAttrValue());
                }else return null;
            }).collect(Collectors.toList());
            list.add(new SpuItemAttrGroupVo(attrGroupName, attrs));
        }
//        //查出当前spu对应的所有属性的分组信息，以及当前分组下所有属性对应的值
//        List<ProductAttrValueEntity> productAttrValueEntities = productAttrValueService.getBaseMapper().selectList(new QueryWrapper<ProductAttrValueEntity>().eq("spu_id", spuId));
//        List<Attr> attrs = productAttrValueEntities.stream().map(item -> new Attr(item.getAttrId(), item.getAttrName(), item.getAttrValue())).collect(Collectors.toList());
//        //根据分类id查出属性分组名
//        List<AttrGroupEntity> attrGroupEntities = this.getBaseMapper().selectList(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catalogId));
//        List<AttrGroupNameIdVo> attrGroupNameIdVos = attrGroupEntities.stream().map(item -> {
//            AttrGroupNameIdVo attrGroupNameIdVo = new AttrGroupNameIdVo();
//            attrGroupNameIdVo.setAttrGroupName(item.getAttrGroupName());
//            attrGroupNameIdVo.setAttrGroupId(item.getAttrGroupId());
//            return attrGroupNameIdVo;
//        }).collect(Collectors.toList());
//        //attrGroupId
//        List<Long> attrGroupIds = attrGroupEntities.stream().map(AttrGroupEntity::getAttrGroupId).collect(Collectors.toList());
//
//        List<List<AttrGroupVo>> attrGroups = attrGroupIds.stream().map(item -> {
//            List<AttrAttrgroupRelationEntity> attrGroup = attrAttrgroupRelationService.getBaseMapper().selectList(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_group_id", item));
//            return attrGroup.stream().map(item0 -> {
//                AttrGroupVo attrGroupVo = new AttrGroupVo();
//                attrGroupVo.setAttrGroupId(item0.getAttrGroupId());
//                attrGroupVo.setAttrId(item0.getAttrId());
//                return attrGroupVo;
//            }).collect(Collectors.toList());
//        }).collect(Collectors.toList());
//
//        System.out.println("attrGroups: "+attrGroups);
//        System.out.println(attrGroups.size());
//        System.out.println("attrs: "+attrs);
//        System.out.println(attrs.size());
//        System.out.println("group name: "+attrGroupNameIdVos);
//        System.out.println(attrGroupNameIdVos.size());
//        List<SpuItemAttrGroupVo> list = new ArrayList<>();
////        for (Attr attr : attrs) {
////            SpuItemAttrGroupVo spuItemAttrGroupVo = new SpuItemAttrGroupVo();
////            Long attrId = attr.getAttrId();
////            AttrAttrgroupRelationEntity entity = attrAttrgroupRelationService.getBaseMapper().selectOne(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_id", attrId));
////            Long attrGroupId = entity.getAttrGroupId();
////            AttrGroupEntity attrGroupEntity = this.getBaseMapper().selectOne(new QueryWrapper<AttrGroupEntity>().eq("attr_group_id", attrGroupId));
////            spuItemAttrGroupVo.setGroupName(attrGroupEntity.getAttrGroupName());
////
////
////            spuItemAttrGroupVo.setAttrs();
////            list.add(spuItemAttrGroupVo);
////        }
//        for (AttrGroupNameIdVo attrGroupNameIdVo : attrGroupNameIdVos) {
//            SpuItemAttrGroupVo spuItemAttrGroupVo = new SpuItemAttrGroupVo();
//            spuItemAttrGroupVo.setGroupName(attrGroupNameIdVo.getAttrGroupName());
//            Long attrGroupId = attrGroupNameIdVo.getAttrGroupId();
//            List<AttrAttrgroupRelationEntity> attrgroupRelationEntities = attrAttrgroupRelationService.getBaseMapper().selectList(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_group_id", attrGroupId));
//            attrgroupRelationEntities.stream().map(item->{
//                Long attrId = item.getAttrId();
//                this.getBaseMapper().selectOne(new QueryWrapper<AttrGroupEntity>().eq("attr_"))
//            })
//        }
        return list;
    }
}